<%--
  Created by IntelliJ IDEA.
  User: 27716
  Date: 2024/10/28
  Time: 19:11
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>链表</title>
    <script src="../../static/js/jquery-1.8.2.min.js"></script>
    <link rel="stylesheet" href="../../static/css/css.css">
    <script>
        $(function (){
            //1.取值 2.组装对象 3.ajax
            let goodsName = $("#goodsName").val();
            let obj = {goodsName:goodsName}
            getGoodsList(obj);
        })

        function getGoodsList(obj){
            $.ajax({
                url:"/goods/getGoodsList",
                type:"post",
                data:{reqInfos:JSON.stringify(obj)},
                dataType:"json",
                success(res){
                    console.log(res);
                    //1.取值 数据 上一页，下一页，尾页
                    let arr = res.data;
                    $("#table").empty();
                    $("#table").append(`
                    <tr>
                        <td>
                            <input type="checkbox" id="check">
                        </td>
                        <td>商品编号id</td>
                        <td>商品编码</td>
                        <td>商品名称</td>
                        <td>库存数量</td>
                        <td>采购价格</td>
                        <td>备注</td>
                        <td>商品名</td>
                        <td>供应商名</td>
                        <td>操作</td>
                    </tr>
                    `)
                    for (let o of arr) {
                        $("#table").append(`
                        <tr>
                                <td>
                                    <input type="checkbox" class="ck" value="\${o.goodsId}">
                                </td>
                                <td>\${o.goodsId}</td>
                                <td>\${o.goodsCode}</td>
                                <td>\${o.goodsName}</td>
                                <td>\${o.inventoryQuantity}</td>
                                <td>\${o.purchasingPrice}</td>
                                <td>\${o.remarks}</td>
                                <td>\${o.goodsTypeName}</td>
                                <td>\${o.supplierName}</td>
                                <td>
                                    <input type="button" value="删除" onclick="getToDel(\${o.goodsId})">
                                    <input type="button" value="修改" onclick="getToUpd(\${o.goodsId})">
                                </td>

                            </tr>
                        `)
                    }
                    //循环结束之后 追加按钮
                    $("#table").append(`
                        <tr>
                            <td colspan="100">
                                <input type="button" value="首页" onclick="doPage(1)">
                                <input type="button" value="上一页" onclick="doPage(\${res.prePage})">
                                <input type="button" value="下一页" onclick="doPage(\${res.nextPage})">
                                <input type="button" value="尾页" onclick="doPage(\${res.pages})">
                            </td>
                        </tr>
                    `)
                },
                error(){
                    alert("服务器故障")
                }
            })
        }
    </script>
</head>
<body>
<input type="text" id="goodsName">
<input type="button" value="搜索" id="search">
<input type="button" value="批删" id="getToAllDel">
<input type="button" value="添加" id="add">
<table id="table">
    <tr>
        <td>
            <input type="checkbox" id="check">
        </td>
        <td>商品编号id</td>
        <td>商品编码</td>
        <td>商品名称</td>
        <td>库存数量</td>
        <td>采购价格</td>
        <td>备注</td>
        <td>商品名</td>
        <td>供应商名</td>
        <td>操作</td>
    </tr>
</table>
</body>
<script>
    //全选 全不选
    $(document).on('click',"#check",function (){
        $(".ck").prop("checked",this.checked);
    })
    //删除
    function getToDel(goodsId){
        let obj = {goodsUid:goodsId};
        $.ajax({
            url:"/goodschange/getToDel",
            type:"post",
            data: {reqInfos:JSON.stringify(obj)},
            dataType: "json",
            success(res) {
                if(res===200){
                    alert("删除成功");
                    location.reload();
                }
            },
            error() {
                alert("数据无法查看")
            }
        })
    }

    //批删除
    $("#getToAllDel").click(function (){
        let arr = [];
        $(".ck:checked").each(function () {
            let uid = $(this).val();
            arr.push(uid);
        })
        let s = arr.join(",");
        if(!s){
            alert("数据不能为空");
            return
        }
        getToDel(s);
    })
    //搜索框
    $("#search").click(function (){
        let goodsName = $("#goodsName").val();
        let obj = {goodsName:goodsName}
        getGoodsList(obj);
    })
    //搜索框
    function doPage(pageNum){
        let goodsName = $("#goodsName").val();
        let obj = {goodsName:goodsName,pageNum:pageNum}
        getGoodsList(obj);
    }
    //添加
    $("#add").click(function (){
        location.href = "adds.jsp";
    })
    //修改
    function getToUpd(goodsId){
        location.href = "upd.jsp?goodsId="+goodsId;
    }
</script>
</html>
